package com.tanhua.sso.controller;

import com.tanhua.common.vo.ErrorResult;
import com.tanhua.sso.service.SmsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @Author 王加鑫
 * @Date 2021/08/31 20:02
 * @Since JDK 1.8
 */
@RestController
@RequestMapping("user")
@Slf4j
public class SmsController {

    @Autowired
    private SmsService smsService;

    /**
     * 发送短信验证码
     */
    @RequestMapping(value = "login")
    public ResponseEntity<ErrorResult> sendCheckCode(@RequestBody Map<String, String> map) {
        String phone = map.get("phone");
        //提升变量的作用域范围
        ErrorResult errorResult = null;
        //调用smsService去做具体验证码的发送
        try {
            errorResult = smsService.sendCheckCode(phone);
            if(null == errorResult){
                //errorResult为null的话说明验证码发送没有出现问题
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            errorResult = ErrorResult.builder().errCode("000001").errMessage("验证码发送错误").build();
            //到这里说明验证码发送出现异常
            log.error("验证码发送错误");
        }
        return ResponseEntity.status(500).body(errorResult);
    }
}
